package com.android.minetopin.utils;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.android.minetopin.MainActivity;
import com.android.minetopin.R;
import com.android.minetopin.activity.BaseObject;
import com.android.minetopin.activity.BaseWeb;
import com.android.minetopin.activity.GoodsDetailActivity;
import com.android.minetopin.activity.GoodsStoreActivity;
import com.android.minetopin.activity.MainTab;
import com.android.minetopin.enums.EJumpStatus;
import com.android.minetopin.fragment.group.Fragment_BrandDiscountMain;
import com.android.minetopin.fragment.group.Fragment_EveryBodyBuy;
import com.android.minetopin.fragment.group.Fragment_Group;
import com.android.minetopin.fragment.group.Fragment_StoreStreet;
import com.android.minetopin.fragment.member.Fragment_SameCity_Service;
import com.android.minetopin.pojo.BannerRegmatchPojo;
import com.android.minetopin.pojo.BannerUintPojo;
import com.android.minetopin.pojo.BroadcastPojo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.android.minetopin.enums.EJumpStatus.EnumGoodkill;

/**
 * Created by Administrator on 2017/11/8.
 */

public class JumpUtils {

    /**
     *
     *
     *
     EnumGoodkill("Goodkill","限时秒杀","Goodkill"),
     EnumSeckill("Seckill","秒杀团","Seckill"),
     EnumFreeTest("FreeTest","免费试用","FreeTest"),
     EnumBrandDiscount("BrandDiscount","品牌折扣","BrandDiscount"),  -- Fragment_Group and BaseObject
     EnumSpread("Spread","高佣推广","Spread"),
     EnumFreePostage("FreePostage","9.9元包邮","FreePostage"),
     EnumHotSail("HotSail","热销榜","HotSail"),
     EnumNeighbor("Neighbor","邻居团","Neighbor"),
     EnumPromote("Promote","推广团","Promote"),
     EnumNewHotSail("NewHotSail","新品热销","NewHotSail"),
     EnumSameCity("SameCity","同城新零售","SameCity"),
     EnumStair("Stair","专题","Stair:id"),
     EnumStore("Store","店铺","Store:id"),
     EnumDetail("Detail","详情","Detail:id"),
     EnumGoodsList("GoodsList","产品列表","GoodsList:id");
     *
     * GetApp().jumpToEveryWhere(getActivity(), Apojo, Fragment1.this);
     *
     * GetApp().jumpToEveryWhere(MainActivity.this, Apojo, null);
     *
     * @param context
     * @param Apojo
     * @param Fragment 这个是Fragment才会有的。 而继承于ommonActivity的把它设为null
     *
     *                 有处理，就返回ture;没处理就返回false
     *
     *
     *
     *
     *
     *
     *
     *                  //Fragment_Group：首页装载的Fragment
     */
    public static boolean jumpToEveryWhere(Context mcontext, BannerUintPojo Apojo, Fragment fragment){
        ReleaseManager.printLog("jumpToEveryWhere", Apojo.toString()+Apojo.return_type);

        if(Apojo.return_type==null||"".equals(Apojo.return_type)) {
           ReleaseManager.printLog("jumpToEveryWhere", "return_type是空的，jumpToEveryWhere不处理！！！！！！");
            return false;

        }


        if(IsRegmatch(Apojo.return_type, EJumpStatus.EnumGoodkill.getRegmatch())) {
            ReleaseManager.printLog(EJumpStatus.EnumGoodkill.getDescription());
            if(fragment!=null&&fragment instanceof Fragment) {
                BannerRegmatchPojo pojo = regMatchOne(Apojo.return_type,EJumpStatus.EnumGoodkill.getRegmatch());
                int retlen =Integer.parseInt(pojo.X1+"");
                Intent intent = new Intent(mcontext, MainTab.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, 0);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 9);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SECONDKILL_TIME, retlen);
                ReleaseManager.printLog("EnumGoodkill-retlen",retlen+"");
                mcontext.startActivity(intent);
                return true;
            }
        } else if(IsRegmatch(Apojo.return_type, EJumpStatus.EnumSeckill.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumSeckill.getDescription());

//Fragment_Group
            if(fragment!=null&&fragment instanceof Fragment) {

                Intent intent_tab = new Intent(mcontext, MainTab.class);
               // intent_tab.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, Apojo.title);
                intent_tab.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, 0);
                intent_tab.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 3);
                fragment.startActivity(intent_tab);
                return true;
            }

        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumFreeTest.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumFreeTest.getDescription());
        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumBrandDiscount.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumBrandDiscount.getDescription());


            if(fragment!=null&&fragment instanceof Fragment) {
                Intent intent_bd = new Intent(mcontext, BaseObject.class);
                intent_bd.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, Apojo.title);
                intent_bd.putExtra(ConstantsUrl.INTENT_KEY.return_type, Apojo.return_type);
                fragment.startActivity(intent_bd);
                return true;
            }else if(mcontext!=null&& mcontext instanceof BaseObject){
                BaseObject baseObject = (BaseObject)mcontext;
                FragmentManager fm =  baseObject.getSupportFragmentManager();
                fm.beginTransaction().replace(R.id.frament, Fragment_BrandDiscountMain.newInstance()).commit();
                return true;
            }


        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumSpread.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumSpread.getDescription());

            if(fragment!=null&&fragment instanceof Fragment) {
                Intent intent_tabs = new Intent(mcontext, MainTab.class);
                intent_tabs.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, 0);
                intent_tabs.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 8);
                fragment.startActivity(intent_tabs);
                return true;
            }
//            else if(mcontext!=null&& mcontext instanceof BaseObject){
//                BaseObject baseObject = (BaseObject)mcontext;
//                FragmentManager fm =  baseObject.getSupportFragmentManager();
//                fm.beginTransaction().replace(R.id.frament, Fragment_BrandDiscountMain.newInstance()).commit();
//                return true;
//            }


        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumFreePostage.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumFreePostage.getDescription());


        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumHotSail.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumHotSail.getDescription());

            if(fragment!=null&&fragment instanceof Fragment) {

                Intent intenthot = new Intent(mcontext, BaseObject.class);
               // intenthot.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "热搜");
                intenthot.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, Apojo.title);
                intenthot.putExtra(ConstantsUrl.INTENT_KEY.return_type, Apojo.return_type);
                fragment.startActivity(intenthot);
                return true;
            }else if(mcontext!=null&& mcontext instanceof BaseObject){
                BaseObject baseObject = (BaseObject)mcontext;
                FragmentManager fm =  baseObject.getSupportFragmentManager();
                fm.beginTransaction().replace(R.id.frament, Fragment_EveryBodyBuy.newInstance()).commit();
                return true;
            }



        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumNeighbor.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumNeighbor.getDescription());
        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumPromote.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumPromote.getDescription());
        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumNewHotSail.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumNewHotSail.getDescription());
            if(mcontext!=null&& mcontext instanceof MainActivity){
                MainActivity A1 = (MainActivity)mcontext;
                A1.ChangeMainFragment(1);
                return true;
            }

        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumSameCity.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumSameCity.getDescription());

            if(fragment!=null&&fragment instanceof Fragment_Group) {

                Fragment_Group fragment_Group  =(Fragment_Group)fragment;
                fragment_Group.JumptoSameCity(Apojo.return_type);
                return true;
            }else if(mcontext!=null&& mcontext instanceof BaseObject){
                BaseObject baseObject = (BaseObject)mcontext;
                FragmentManager fm =  baseObject.getSupportFragmentManager();
                baseObject.main_top_citychoose.setVisibility(View.VISIBLE);
                fm.beginTransaction().replace(R.id.frament, Fragment_SameCity_Service.newInstance()).commit();

                return true;
            }

        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumStair.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumStair.getDescription());

            if(fragment!=null&&fragment instanceof Fragment) {

                BannerRegmatchPojo pojo = regMatchOne(Apojo.return_type,EJumpStatus.EnumStair.getRegmatch());
                Intent intent_bd = new Intent(mcontext, BaseObject.class);
                intent_bd.putExtra("topicname", Apojo.title);
                intent_bd.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID,pojo.X1+"");
                intent_bd.putExtra(ConstantsUrl.INTENT_KEY.return_type, Apojo.return_type);
                fragment.startActivity(intent_bd);


                return true;
            }else if(mcontext!=null&& mcontext instanceof BaseObject){
                BaseObject baseObject = (BaseObject)mcontext;
                baseObject.jumptoStair();
                return true;
            }

        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumStore.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumStore.getDescription());
            if(fragment!=null&&fragment instanceof Fragment) {
                BannerRegmatchPojo pojo = regMatchOne(Apojo.return_type,EJumpStatus.EnumStore.getRegmatch());
                Intent intent = new Intent(mcontext, GoodsStoreActivity.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_STORE_ID, pojo.X1 + "");
                fragment.startActivity(intent);
                return true;
            }

        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumDetail.getRegmatch())){
            BannerRegmatchPojo pojo = regMatchOne(Apojo.return_type,EJumpStatus.EnumDetail.getRegmatch());
            ReleaseManager.printLog(EJumpStatus.EnumDetail.getDescription());
            Intent intent = new Intent(mcontext, GoodsDetailActivity.class);
            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ID, pojo.X1+"");
            mcontext.startActivity(intent);
            return true;

        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EGoodsList.getRegmatch())){
            // 首页的上部的切换 ---Fragment_SAMain_TabList里好像也有
            ReleaseManager.printLog(EJumpStatus.EGoodsList.getDescription());
            if(fragment!=null&&fragment instanceof Fragment) {
                BannerRegmatchPojo pojo = regMatchOne(Apojo.return_type,EJumpStatus.EGoodsList.getRegmatch());
               // Fragment_Group fragment_Group = (Fragment_Group)fragment; //bug可能是Fragment_SAMain_TabList里好像也有
                 //设置id为几的viewpager的切换；在fragment_Group里写

                if(mcontext!=null && mcontext instanceof MainActivity){

                    Intent intent_tab = new Intent(mcontext, MainTab.class);
                    //表示切换到哪个tab
                 //   intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, (pos + 1));
                    intent_tab.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, 0);
                    intent_tab.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE,2);
                    intent_tab.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE_SERACH, pojo.X1+"");
                    intent_tab.putExtra("noneedtop",1);
                    mcontext.startActivity(intent_tab);
//                    MainActivity mainActivity = (MainActivity)mcontext;
//
//                    BroadcastPojo broadcastPojo = new BroadcastPojo();
//                    broadcastPojo.type = 1;
//                    broadcastPojo.X1 = pojo.X1;
//
//                    mainActivity.ChangeMainFragmentTabHost(0,broadcastPojo);
                }
                return true;
            }


        }else if(IsRegmatch(Apojo.return_type,EJumpStatus.EnumShopStreet.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.EnumShopStreet.getDescription());
            if(fragment!=null&&fragment instanceof Fragment) {

                Intent intent_bd = new Intent(mcontext, BaseObject.class);
                intent_bd.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, Apojo.title);
                intent_bd.putExtra(ConstantsUrl.INTENT_KEY.return_type, Apojo.return_type);
                fragment.startActivity(intent_bd);
                return true;
            }else if(mcontext!=null&& mcontext instanceof BaseObject){
                BaseObject baseObject = (BaseObject)mcontext;
                FragmentManager fm =  baseObject.getSupportFragmentManager();
                fm.beginTransaction().replace(R.id.frament, Fragment_StoreStreet.newInstance()).commit();

                return true;
            }

        }



        //本地的判断
        else if(IsRegmatch(Apojo.return_type,EJumpStatus.ECountry.getRegmatch())){
            ReleaseManager.printLog(EJumpStatus.ECountry.getDescription());
//Fragment_SAMain_TabList
            if(fragment!=null&&fragment instanceof Fragment) {

                Intent intent_country = new Intent(mcontext, BaseObject.class);
                intent_country.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE,"国家馆");
                intent_country.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, Apojo.id);
                intent_country.putExtra("country_name", Apojo.title);
                fragment.startActivity(intent_country);

                return true;
            }else if(mcontext!=null&& mcontext instanceof BaseObject){
                BaseObject baseObject = (BaseObject)mcontext;
                baseObject.JumptoCoutry();
                return true;
            }

        }else if (IsRegmatch(Apojo.return_type, EJumpStatus.EnumWebView.getRegmatch())) {
            ReleaseManager.printLog(EJumpStatus.EnumWebView.getDescription());
            Intent intent = new Intent(mcontext, BaseWeb.class);
            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_WEB_URL, Apojo.return_type);
            mcontext.startActivity(intent);
            return true;
        }

       /* if(EJumpStatus.WEB_PAGE.getCode().equals(Apojo.return_type)){
            if(Fragment!=null){
                Fragment.jumpToCommonWeb(Apojo.title, Apojo.return_config);
            }else if(context instanceof CommonActivity){
                CommonActivity common = (CommonActivity)context;
                common.jumpToCommonWeb(Apojo.title, Apojo.return_config);
            }else if(context instanceof FragmentBottomTabPager){
                FragmentBottomTabPager common = (FragmentBottomTabPager)context;
                common.jumpToCommonWeb(Apojo.title, Apojo.return_config);
            }else{
                jumpToCommonWeb(Apojo.title, Apojo.return_config);
            }

        }
        else  if(EJumpStatus.Special_recommendation.getCode().equals(Apojo.return_type)){

            CategoryVoucherUintPojo pojo = new CategoryVoucherUintPojo();
            pojo.id=Apojo.return_config;
            pojo.name=Apojo.title;
            Intent intent = new Intent(context, CategorysGoodsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("category", pojo);
            intent.putExtras(bundle);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);


        }*/
        ReleaseManager.printLog("jumpToEveryWhere", "jumpUtils里没有处理！！！！！！");
        return false;
    }


    public static BannerRegmatchPojo regMatchOne(String withinText, String regString) {
       return regMatch(withinText,regString,1);
    }

    /**
     *  true表示匹配
     *  false表示不匹配
     * @param withinText
     * @param regString
     * @return
     */
    public static boolean IsRegmatch(String withinText, String regString) {
        Pattern pattern = Pattern.compile(regString);
        Matcher matcher = pattern.matcher(withinText);
        if (matcher.find()) {
            ReleaseManager.printLog("匹配类型  " + withinText);
            return  true;
        }
        return false;

    }
    /**
     *
     * @param withinText 被匹配的正则
     * @param regString 正则表达式
     * @param matchNumber 要匹配的个数
     * @return
     */
    public static BannerRegmatchPojo regMatch(String withinText, String regString, int matchNumber) {
        BannerRegmatchPojo pojo = null;
        Pattern pattern = Pattern.compile(regString);
        Matcher matcher = pattern.matcher(withinText);
        if (matcher.find()) {

            pojo = new BannerRegmatchPojo();

            matcher.reset();
            while (matcher.find()) {
                switch (matchNumber) {
                    case 1:
                        pojo.X0 = matcher.group(0);
                        ReleaseManager.printLog("0匹配  " + pojo.X0);
                        pojo.X1 = matcher.group(1);
                        ReleaseManager.printLog("1匹配  " + pojo.X1);

                        break;
                    case 2:
                        pojo.X0 = matcher.group(0);
                        ReleaseManager.printLog("0匹配    " + pojo.X0);
                        pojo.X1 = matcher.group(1);
                        ReleaseManager.printLog("1匹配    " + pojo.X1);
                        pojo.X2 = matcher.group(2);
                        ReleaseManager.printLog("2匹配    " + pojo.X2);
                        break;
                    case 3:
                        pojo.X0 = matcher.group(0);
                        ReleaseManager.printLog("0匹配    " + pojo.X0);
                        pojo.X1 = matcher.group(1);
                        ReleaseManager.printLog("1匹配    " + pojo.X1);
                        pojo.X2 = matcher.group(2);
                        ReleaseManager.printLog("2匹配    " + pojo.X2);
                        pojo.X3 = matcher.group(3);
                        ReleaseManager.printLog("3匹配    " + pojo.X3);
                        break;
                    default:
                        break;
                }

            }
        }else{
            ReleaseManager.printLog("正则匹配失败！！！！！！！！！！！！！");
        }
        return pojo;
    }


}
